/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.core;

import java.util.ArrayList;
import java.util.List;
import org.nop.core.Request;
import org.nop.templating.Template;
import org.nop.templating.XmlTreeBuilder;
import org.nop.util.Injected;
import org.xthl.core.api.MenuCustomItem;
import org.xthl.core.api.MenuItem;
import org.xthl.core.api.MenuLogic;
import org.xthl.core.api.MenuSpecialItem;
import org.xthl.core.lang.LabelsBundle;
import org.xthl.core.view.MenuItemSight;
import org.xthl.core.view.MenuView;

/**
 *
 * @author Alexey Andreev
 */
public class MenuBlock extends AbstractBlock {
    private Request request;
    private MenuLogic logic;

    @Injected
    public MenuBlock(Request request, MenuLogic logic) {
        this.request = request;
        this.logic = logic;
    }
    
    @Override
    public boolean isVisible() {
        return true;
    }

    @Override
    public Template getView() {
        List<MenuItem> items = logic.getEnabledItems();
        List<MenuItemSight> itemSigths = new ArrayList<MenuItemSight>();
        for (MenuItem item : items) {
            if (!item.isEnabled()) {
                continue;
            }
            if (item instanceof MenuCustomItem) {
                final MenuCustomItem custom = (MenuCustomItem)item;
                MenuItemSight sight = new MenuItemSight();
                sight.setLink(custom.getUrl());
                sight.setTitle(new Template() {
                    @Override
                    public void generate(XmlTreeBuilder treeBuilder) {
                        treeBuilder.addText(custom.getTitle());
                    }
                });
                itemSigths.add(sight);
            } else if (item instanceof MenuSpecialItem) {
                final MenuSpecialItem special = (MenuSpecialItem)item;
                if (special.getGeneratorType() == null) {
                    continue;
                }
                MenuItemRenderer itemGen = (MenuItemRenderer)request.create(
                        special.getGeneratorType());
                if (!itemGen.isVisible()) {
                    continue;
                }
                MenuItemSight sight = new MenuItemSight();
                sight.setLink(itemGen.getLink());
                if (special.getTitle().isEmpty()) {
                    sight.setTitle(itemGen.getTitle());
                } else {
                    sight.setTitle(new Template() {
                        @Override
                        public void generate(XmlTreeBuilder treeBuilder) {
                            treeBuilder.addText(special.getTitle());
                        }
                    });
                }
                itemSigths.add(sight);
            }
        }
        return createView(MenuView.class).setItems(itemSigths);
    }

    @Override
    public Template getTitle() {
        return bundle(LabelsBundle.class).menuBlockTitle();
    }
}
